package user

import (
	"encoding/json"
	"fmt"
	"github.com/DanPlayer/randomname"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"shangyuapi/service"
	"shangyuapi/tool"
)

func Imgurl(ctx *gin.Context) {
	config := tool.GetConfig().Oss
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    config.Domain,
		"msg":     "ok",
	})
	return
}

type Cityinfo struct {
	First string        `json:"first" from:"first"`
	List  []models.City `json:"list" from:"list"`
}

func Citylist(ctx *gin.Context) {
	var zimu = [27]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	var citys []models.City
	citys = service.Citys()

	var ciytinfo []Cityinfo
	for _, z := range zimu {
		var list []models.City
		for _, c := range citys {
			if z == c.First {
				list = append(list, c)
			}
		}
		if len(list) > 0 {
			cityinfoon := Cityinfo{
				First: z,
				List:  list,
			}
			ciytinfo = append(ciytinfo, cityinfoon)
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    ciytinfo,
		"msg":     "ok",
	})
	return
}

type CitysearchBy struct {
	Name string `json:"name" from:"name"`
}

func Citysearch(ctx *gin.Context) {
	request := CitysearchBy{}
	ctx.ShouldBind(&request)
	name := request.Name

	var city models.City
	data_source.Db.Where("name like ?", "%"+name+"%").Where("type = 2").First(&city)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    city,
		"msg":     "ok",
	})
	return
}

func Provinces(ctx *gin.Context) {

	var citys []models.City
	citys = service.Citylist()

	result := getListGoods(citys, 0, 1)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    result,
		"msg":     "ok",
	})
	return
}

func getListGoods(stuAll []models.City, pid int64, lev int64) []models.City {
	var goodArr []models.City
	for _, v := range stuAll {
		if v.Pid == pid {
			// 这里可以理解为每次都从最原始的数据里面找出相对就的ID进行匹配，直到找不到就返回
			child := getListGoods(stuAll, v.ID, lev+1)
			node := models.City{
				ID:    v.ID,
				Name:  v.Name,
				Type:  lev,
				First: v.First,
				Remen: v.Remen,
				List:  child,
			}
			goodArr = append(goodArr, node)
		}
	}
	return goodArr
}

func Arranges(ctx *gin.Context) {
	var users []models.Arrange
	data_source.Db.Order("id asc").Find(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return
}
func Biaoqians(ctx *gin.Context) {
	var admin []models.Biaoqian
	data_source.Db.Find(&admin)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    admin,
		"msg":     "ok",
	})
	return
}

type Suijibody struct {
	Nickname string `json:"nickname" from:"nickname"`
	Logo     string `json:"logo" from:"logo"`
}

func Suiji(ctx *gin.Context) {
	nickname := randomname.GenerateName()

	var logos models.Logo
	data_source.Db.Order("RAND()").First(&logos)
	data := Suijibody{
		Nickname: nickname,
		Logo:     logos.Image,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type dizhi struct {
	Status int64  `json:"status"`
	Result Result `json:"result"`
}
type Result struct {
	AddressComponent AddressComponent `json:"address_component"`
}
type AddressComponent struct {
	Nation   string `json:"nation"`
	Province string `json:"province"`
	City     string `json:"city"`
}
type CityBody struct {
	Lng string `json:"lng" from:"lng"`
	Lat string `json:"lat" from:"lat"`
}

func City(ctx *gin.Context) {
	request := CityBody{}
	ctx.ShouldBind(&request)
	lng := request.Lng
	lat := request.Lat

	url := "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=XPYBZ-5O437-K2TXR-HYHHB-H3TMF-HDFGS"
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if resp.StatusCode == 200 {
		var res dizhi
		_ = json.Unmarshal(body, &res)
		cityname := res.Result.AddressComponent.City
		var city models.City
		data_source.Db.Where("name = ?", cityname).Where("type = 2").First(&city)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    city,
			"msg":     "ok",
		})
		return
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "获取失败",
		})
		return
	}
}
